package com.cyt.gen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.gen.dao.FormFieldDao;
import com.cyt.gen.model.po.FormField;
import com.cyt.gen.model.vo.FormFieldVo;
import com.cyt.gen.model.vo.FormTableFieldVo;
import com.cyt.gen.model.vo.FormTableVo;
import com.cyt.gen.service.FormFieldService;
import com.cyt.gen.service.FormTableService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysDictVo;
import com.cyt.sys.service.SysDictService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单自定义-搜索栏信息Service层实现类
 *
 * @author rong yuan
 * @date 2019-4-16 8:44:39
 */
@Service("formFieldService")
public class FormFieldServiceImpl extends BaseServiceImpl<FormField> implements FormFieldService {

    @Resource
    private FormFieldDao formFieldDao;
    @Resource
    private FormTableService formTableService;
    @Resource
    private SysDictService sysDictService;

    @Autowired
    public void setBaseDao(FormFieldDao formFieldDao) {
        this.baseDao = formFieldDao;
    }

    private Example initExample(FormFieldVo formFieldVo) {
        Example example = new Example(FormField.class);
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(formFieldVo.getTableName())) {
            criteria.andEqualTo("tableName", formFieldVo.getTableName());
        }
        if (StrUtil.isNotEmpty(formFieldVo.getClassify())) {
            criteria.andEqualTo("classify", formFieldVo.getClassify());
        }
        return example;
    }

    /**
     * 添加或更新
     *
     * @param formFieldVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-4-16 8:44:39
     */
    @Override
    public ReturnResult addOrUpdate(FormFieldVo formFieldVo, SysUser sysUser) {
        FormField formField = new FormField();
        formField.setId(BaseUtil.uId());
        formField.setTableName(formFieldVo.getTableName());
        formField.setField(formFieldVo.getField());
        formField.setName(formFieldVo.getName());
        formField.setOrderNum(formFieldVo.getOrderNum());
        formField.setType(formFieldVo.getType());
        formField.setCode(formFieldVo.getCode());
        formField.setFormat(formFieldVo.getFormat());
        formField.setIsRelated(formFieldVo.getIsRelated());
        formField.setRelatedTable(formFieldVo.getRelatedTable());
        formField.setRelatedField(formFieldVo.getRelatedField());
        formField.setRelatedShowField(formFieldVo.getRelatedShowField());
        formField.setClassify(formFieldVo.getClassify());
        formField.setNotBlank(formFieldVo.getNotBlank());
        formField.setSingleRow(formFieldVo.getSingleRow());
        int num = formFieldDao.insert(formField);
        if (num <= 0) {
            return new ReturnResult(ErrorCode.CODE_1, ErrorCode.CODE_ADD_FAIL_MSG);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }

    /**
     * 查询对象集合
     *
     * @param formFieldVo 对象
     * @return List 对象集合
     * @author rong yuan
     * @date 2019-4-16 8:44:39
     */
    @Override
    public List<FormFieldVo> list(FormFieldVo formFieldVo) {
        List<FormFieldVo> list = formFieldDao.selectByCondition(formFieldVo);
        if (list != null && list.size() > 0) {
            List<FormTableVo> formTableVoList = null;
            List<FormTableFieldVo> formTableFieldVoList = null;
            for (FormFieldVo vo : list) {
                if ("1".equals(vo.getIsRelated())) {
                    if (formTableVoList == null) {
                        formTableVoList = formTableService.listTable(new FormTableVo());
                    }
                    vo.setFormTableVoList(formTableVoList);
                    if (formTableFieldVoList == null) {
                        FormTableVo formTableVo = new FormTableVo();
                        formTableVo.setTableName(vo.getRelatedTable());
                        formTableFieldVoList = formTableService.listTableField(formTableVo);
                    }
                    vo.setFormTableFieldVoList(formTableFieldVoList);
                } else if ("select".equals(vo.getType())) {
                    vo.setSysDictList(sysDictService.listExample(new SysDictVo()));
                }
            }
        }
        return list;
    }

    /**
     * 删除数据
     *
     * @param formFieldVo 参数接受对象
     * @author rong yuan
     * @date 2019/4/17 14:19
     */
    @Override
    public void deleteByExample(FormFieldVo formFieldVo) {
        formFieldDao.deleteByExample(initExample(formFieldVo));
    }

    /**
     * 查询关联字段信息
     *
     * @param formFieldVo 参数接受对象
     * @return String
     * @author rong yuan
     * @date 2019/4/22 10:09
     */
    @Override
    public String selectRelateInfo(FormFieldVo formFieldVo) {
        formFieldVo.setIsRelated("1");
        List<FormFieldVo> formFieldVoList = formFieldDao.selectByCondition(formFieldVo);
        if (formFieldVoList == null || formFieldVoList.size() == 0) {
            return "";
        }

        int tableIndex = 0;
        StringBuilder relateInfo = new StringBuilder();
        for (FormFieldVo vo : formFieldVoList) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(vo.getRelatedTable()).append(":");
            stringBuilder.append(vo.getRelatedField()).append(":");
            stringBuilder.append(vo.getRelatedShowField()).append(":");
            if (StringUtils.isBlank(relateInfo.toString()) || !relateInfo.toString().contains(stringBuilder.toString())) {
                relateInfo.append(BaseUtil.toTfmm(vo.getField())).append(":");
                relateInfo.append(vo.getName()).append(":");
                if (relateInfo.toString().contains(vo.getRelatedTable())) {
                    int index = relateInfo.toString().indexOf(vo.getRelatedTable());
                    relateInfo.append(relateInfo.toString().substring((index - 3), (index - 1))).append(":");
                } else {
                    tableIndex++;
                    relateInfo.append("t").append(tableIndex).append(":");
                }
                relateInfo.append(vo.getRelatedTable()).append(":");
                relateInfo.append(vo.getRelatedField()).append(":");
                relateInfo.append(vo.getRelatedShowField()).append(":");
                relateInfo.append(BaseUtil.toUpperCaseFirstOne(BaseUtil.toTfmm(vo.getRelatedShowField()))).append(":");
                relateInfo.append(vo.getField()).append(";");
            }
        }
        return relateInfo.substring(0, relateInfo.length() - 1);
    }

    /**
     * 查询非关联字段信息
     *
     * @param formFieldVo 参数接受对象
     * @return String
     * @author rong yuan
     * @date 2019/4/22 10:09
     */
    @Override
    public String selectNotRelateInfo(FormFieldVo formFieldVo) {
        formFieldVo.setIsRelated(null);
        StringBuilder stringBuilder = new StringBuilder();
        List<FormFieldVo> formFieldVoList = formFieldDao.selectByCondition(formFieldVo);
        if (formFieldVoList != null && formFieldVoList.size() > 0) {
            for (FormFieldVo vo : formFieldVoList) {
                if (!"1".equals(vo.getIsRelated())) {
                    stringBuilder.append(vo.getField()).append(":");
                    stringBuilder.append(BaseUtil.toTfmm(vo.getField())).append(":");
                    stringBuilder.append(vo.getType()).append(":");
                    stringBuilder.append(BaseUtil.toUpperCaseFirstOne(BaseUtil.toTfmm(vo.getField()))).append(":");
                    stringBuilder.append(vo.getFormat()).append(";");
                }
            }
        }
        if (stringBuilder.length() == 0) {
            return "";
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }
}